var app = getApp();
const checkLogin = require('../../utils/checkLogin.js')
const api = require('../../APIConfig/apiConfig.js');
const wxHttpHandler = require('../../utils/wxHttpHandler.js');
Page({
  /**
   * 页面的初始数据
   */
  data: {
    coldBoxAlarm: 0, //冷箱报警
    coldCarAlarm: 0, //冷车报警
    coldHouseAlarm: 0, //冷库报警
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.coldchainAlarmRemindSettingList()
  },


  // 获取设置
  coldchainAlarmRemindSettingList: function () {
    let data = {
      userId: wx.getStorageSync('id')
    }
    const that = this
    wxHttpHandler({
      type: 'get',
      path: api.coldchainAlarmRemindSettingList,
      data: data,
      fn: function (res) {
        if (res.code == 0) {
          that.setData({
            coldBoxAlarm: res.data[0].coldBoxAlarm, //冷箱报警
            coldCarAlarm: res.data[0].coldCarAlarm, //冷车报警
            coldHouseAlarm: res.data[0].coldHouseAlarm, //冷库报警
          })
        } else {
          wx.showToast({
            title: res.desc,
            icon: 'none',
            duration: 1500,
            mask: true
          });
        }
      }

    })
  },

  // 提交设置
  coldchainAlarmRemindSetting: function () {
    let data = {
      coldBoxAlarm: this.data.coldBoxAlarm, //冷箱报警
      coldCarAlarm: this.data.coldCarAlarm, //冷车报警
      coldHouseAlarm:this.data.coldHouseAlarm
    }
    const that = this
    wxHttpHandler({
      type: 'get',
      path: api.coldchainAlarmRemindSetting+wx.getStorageSync('id'),
      data: data,
      fn: function (res) {
        if (res.code == 0) {
         
        } else {
          wx.showToast({
            title: res.desc,
            icon: 'none',
            duration: 1500,
            mask: true
          });
        }
      }

    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {},


  //冷箱报警
  coldBoxAlarmChannge: function (e) {
    if (e.detail.value) {
      this.setData({
        coldBoxAlarm: 1
      })
    } else {
      this.setData({
        coldBoxAlarm: 0
      })
    }
  },

  //冷车报警
  coldCarAlarmChannge: function (e) {
    console.log(e.detail.value)
    if (e.detail.value) {
      this.setData({
        coldCarAlarm: 1
      })
    } else {
      this.setData({
        coldCarAlarm: 0
      })
    }
  },

  //冷库报警
  coldHouseAlarmChannge: function (e) {
    if (e.detail.value) {
      this.setData({
        coldHouseAlarm: 1
      })
    } else {
      this.setData({
        coldHouseAlarm: 0
      })
    }
  },

  onUnload:function(){
    this.coldchainAlarmRemindSetting()
  }


})